home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1397 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.3 KB  |  50 lines

  1. Newsgroups: comp.lang.c++
  2. Path: txnews.amd.com!news
  3. From: Bret Patterson <faustus>
  4. Subject: RTTI 
  5. Content-Type: text/plain; charset=us-ascii
  6. Message-ID: <DKzEnw.3vK@txnews.amd.com>
  7. Sender: news@txnews.amd.com
  8. Nntp-Posting-Host: fuggles
  9. Content-Transfer-Encoding: 7bit
  10. Organization: Advanced Micro Devices, Inc., Austin, Texas, USA
  11. Mime-Version: 1.0
  12. Date: Wed, 10 Jan 1996 20:09:30 GMT
  13. X-Mailer: Mozilla 1.12 (X11; I; HP-UX A.09.05 9000/715)
  14. X-Url: news:comp.lang.c++
  15.  
  16. I've just discovered the ability of c++ to do Run Time Type Identification
  17. using the typeid() class. What I'm wondering is there a way to determine
  18. if a class is  derived from another class?
  19.  
  20. For example:
  21.  
  22. class a {};
  23.  
  24. class b : a {};
  25.  
  26. class c : a {}
  27.  
  28. class d : b {};
  29.  
  30. then I have a function:
  31.  
  32. void doSomething (a *me)
  33. {
  34.   if (typeid(me) == derivedFrom(b)) 
  35.   { doSomethingAlso); };
  36.   else return;
  37. };
  38.  
  39. If I pass doSomething an object that was derived from the b branch
  40. rather than the c branch it will do something special, or likewise.
  41.  
  42. This can be of obvious use where you have a hierarchy of classes where
  43. human is the base class and then you spawn off to water dwelling and 
  44. land dwelling and air dwelling. Each with subclasses. This way if
  45. a human wanted to fly all I would need to do is check if it was
  46. derived from the air dwelling subtree.
  47.  
  48. Bret
  49.  
  50.